# -*- coding: utf-8 -*-
"""
@author: hunan
@date: 2024-12-08
@description: 
"""
import threading

from fastapi import APIRouter, Request

from product_upload.core.amazon_us import batch_tag_amazon, batch_search_words, gen_delete_amazon_us_good_txt, thread_gen_amazon_field_json_and_update_db, update_amazon_quantity_to_txt, update_amazon_price_to_txt, update_amazon_price_quantity_to_txt
from product_upload.core.basic import basic_brand_sensitive_to_write_json_file, basic_sync_product_to_image_to_s3_ocr, sync_image_to_product
from product_upload.domain.basic.basic_product import BasicProduct
from product_upload.util.basic.common_util import extract_and_merge_json, check_time_in_hour
from product_upload.util.basic.mysql_util import db_list_by_page
from product_upload.util.supplier.doba_util import doba_update_price_and_quantity, doba_update_ship_fee
from product_upload.util.supplier.giga_api_util import flush_api_gg_quantity, flush_api_gg_price
from product_upload.util.supplier.giga_rpa_util import basic_gg_transform_to_basic_product
from product_upload.util.supplier.saleyee_util import sy_update_price_quantity
from product_upload.util.supplier.xl_util import parse_csv_and_flush_db

router = APIRouter()


# @router.post("/api/amazon_us/gen_amazon_to_xlsm.do")
# async def api_amazon_us_gen_amazon_to_xlsm(request: Request):
#     req_body = await request.body()
#     params = extract_and_merge_json(req_body.decode('utf-8'))
#     opt_name = params.get("opt_name", "")
#     brand = params.get("brand", "")
#     brand_zip = params.get("brand_zip", "")
#     shop = params.get("shop", "")
#     not_class_name_list = params.get("not_class_name_list", [])
#     product_type = params.get("product_type", "")
#     platform = params.get("platform", "")
#     upload_id = params.get("upload_id", "")
#     price = params.get("price", 30)
#     if not price:
#         price = 30
#     limit_img = params.get("limit_img", 3)
#     if limit_img <= 0:
#         limit_img = 3
#     upload_limit = params.get("upload_limit", 20)
#     print("gen xlsm params:", params)
#     threading.Thread(target=gen_amazon_to_xlsm, args=(
#     opt_name, brand, not_class_name_list, platform, brand_zip, shop, upload_id, upload_limit, limit_img, price)).start()
#     return {"success": True}


@router.post("/api/amazon_us/tag.do")
def api_amazon_us_tag():
    threading.Thread(target=batch_tag_amazon).start()
    return {"success": True}


@router.post("/api/amazon_us/search_words.do")
def api_amazon_us_search_words():
    threading.Thread(target=batch_search_words).start()
    return {"success": True}


@router.post("/api/gg/update/product_info.do")
def api_gg_update_product_info():
    threading.Thread(target=basic_gg_transform_to_basic_product).start()
    return {"success": True}


@router.post("/api/gg/update/price_quantity.do")
def api_gg_update_price_quantity():
    threading.Thread(target=flush_api_gg_quantity).start()
    threading.Thread(target=flush_api_gg_price).start()
    return {"success": True}


@router.post("/api/xl/update/quantity_price.do")
def api_xl_update_quantity_price():
    threading.Thread(target=parse_csv_and_flush_db).start()
    return {"success": True}


@router.post("/api/db/update/quantity_price.do")
def api_db_update_quantity_price():
    threading.Thread(target=doba_update_price_and_quantity).start()
    return {"success": True}


@router.post("/api/sy/update/price_quantity.do")
def api_sy_update_quantity_price():
    threading.Thread(target=sy_update_price_quantity, args=(check_time_in_hour(),)).start()
    return {"success": True}


@router.post("/api/db/update/ship_fee.do")
def api_db_update_ship_fee():
    threading.Thread(target=doba_update_ship_fee).start()
    return {"success": True}


@router.post("/api/basic/update/brand_sensitive.do")
def api_basic_update_brand_sensitive():
    threading.Thread(target=basic_brand_sensitive_to_write_json_file).start()
    return {"success": True}


@router.post("/api/basic/append/new_product.do")
def api_basic_sync_product_to_image_to_s3_ocr():
    threading.Thread(target=basic_sync_product_to_image_to_s3_ocr).start()
    return {"success": True}


@router.post("/api/amazon_us/sku_del.do")
def api_amazon_us_sku_del():
    threading.Thread(target=gen_delete_amazon_us_good_txt).start()
    return {"success": True}


@router.post("/api/amazon_us/append_tag.do")
async def api_amazon_us_append_tag(request: Request):
    req_body = await request.body()
    params = extract_and_merge_json(req_body.decode('utf-8'))
    basic_id = params.get("basic_id")
    remark = params.get("remark")
    if basic_id and remark:
        basic_product_list = db_list_by_page("basic_product", ",".join(BasicProduct.fields), f"id = {basic_id}", BasicProduct, 1, 200)
        threading.Thread(target=thread_gen_amazon_field_json_and_update_db, args=(basic_product_list, True, remark.split(","))).start()
    return {"success": True}


@router.post("/api/amazon_us/sync_image_to_product.do")
async def api_amazon_us_sync_image_to_product(request: Request):
    req_body = await request.body()
    params = extract_and_merge_json(req_body.decode('utf-8'))
    basic_id = params.get("basic_id")
    if basic_id:
        threading.Thread(target=sync_image_to_product, args=(None, basic_id)).start()
    return {"success": True}


@router.post("/api/amazon_us/update_price_quantity_to_txt.do")
def api_amazon_us_update_price_quantity_to_txt():
    threading.Thread(target=update_amazon_price_to_txt).start()
    threading.Thread(target=update_amazon_quantity_to_txt).start()
    return {"success": True}

@router.post("/api/amazon_us/update_price_quantity_to_txt_v2.do")
def api_amazon_us_update_price_quantity_to_txt_v2():
    threading.Thread(target=update_amazon_price_quantity_to_txt).start()
    return {"success": True}


if __name__ == '__main__':
    api_amazon_us_update_price_quantity_to_txt()
    pass
